package l1j.server.server.model.damage.magic;

import static l1j.server.server.model.skill.L1SkillId.MIND_BREAK;
import l1j.server.server.ActionCodes;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.model.Instance.L1SummonInstance;
import l1j.server.server.model.damage.L1Dmg;
import l1j.server.server.model.damage.L1MagicOnNpc;
import l1j.server.server.model.damage.L1NpcMagic;
import l1j.server.server.serverpackets.S_DoActionGFX;
import l1j.server.server.templates.L1Skills;
import l1j.server.server.utils.Random;

public class L1MagicDamage_Npc_Npc extends L1Dmg {

	private L1NpcInstance _attacker;

	private L1NpcInstance _target;

	private int _attr;

	public L1MagicDamage_Npc_Npc(L1NpcInstance attacker, L1NpcInstance target,
			int attr) {
		_attr = attr;
		_attacker = attacker;
		_target = target;
	}

	public void commitMagic(L1Character target) {
		if (Random.chance100(mr_reduction_chance[target.getMr() + 100])) {
			mul((100 - mr_reduction_percent[target.getMr() + 100]) / 100.0);
		}
		if (_attr == 1) {// 地屬性
			mul((100 - attr_reduction_percent[target.getEarth() + 300]) / 100.0);
		} else if (_attr == 2) {// 火屬性
			mul((100 - attr_reduction_percent[target.getFire() + 300]) / 100.0);
		} else if (_attr == 4) {// 水屬性
			mul((100 - attr_reduction_percent[target.getWater() + 300]) / 100.0);
		} else if (_attr == 8) {// 風屬性
			mul((100 - attr_reduction_percent[target.getWind() + 300]) / 100.0);
		}
	}
	
	@Override
	public void hurtTarget(boolean actionHurt) {
		commitAttacker(_attacker);
		commitTarget(_target);
		commitMagic(_target);
		_target.receiveDamage(_attacker, getValue());
		if (getValue() > 0 && actionHurt) {
			_target.broadcastPacket(new S_DoActionGFX(_target.getId(),
					ActionCodes.ACTION_Damage));
		}
	}

	@Override
	public void calcDmg() {
		if (_target.isInvincible()) {
			return;
		}
		L1Skills skill = _attacker.getCurrentSkill();
		if (skill == null) {
			zero();
			return;
		}
		if (((_attacker instanceof L1PetInstance) || (_attacker instanceof L1SummonInstance))
				&& ((_target instanceof L1PetInstance) || (_target instanceof L1SummonInstance))) {
			// 目標在安區、攻擊者在安區
			if ((_target.getZoneType() == 1) || (_target.getZoneType() == 1)) {
				zero();
				return;
			}
		}

		_attr = skill.getAttr();

		L1NpcMagic nm = new L1NpcMagic(_attacker, this);
		nm.calcNpcDmg();

		int skillId = _attacker.getCurrentSkill().getId();
		// 心靈破壞
		if (skillId == MIND_BREAK && _target.getCurrentMp() >= 5) {
			_target.setCurrentMp(_target.getCurrentMp() - 5);
			add(_attacker.getWis() * 5);
		}

		L1MagicOnNpc mon = new L1MagicOnNpc(_target, this);
		mon.calcDmgOnNpc();
	}

}
